package org.xiaojinlong.playground;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Jin Long
 *         2016/4/21
 */
public class Tool {
    private static List<String> fromD1ToD2(LocalDateTime d1, LocalDateTime d2) {
        List<String> result = new ArrayList<>();

        LocalDateTime temp = d1;
        DateTimeFormatter sdf = DateTimeFormatter.ofPattern("yyyy-M-d");
        while (!temp.isAfter(d2)) {
            result.add(temp.format(sdf));
            temp = temp.plusDays(1);
        }
        return result;
    }

    public static void main(String[] args) {
        String[] times = "2015/06/01/ 00:00:00 - 2018/01/01/ 00:00:00".split(" - ");

        DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy/MM/dd/ HH:mm:ss");

        LocalDateTime d1, d2;

        d1 = LocalDateTime.parse(times[0], f);
        d2 = LocalDateTime.parse(times[1], f);
        List<String> dateList = fromD1ToD2(d1, d2);
        String str = "";
        String last = "";

        for (String dateString : dateList) {
            String dateString2 = last.replace("-", "_");
            str += "PARTITION p" + dateString2 + " VALUES LESS THAN (TO_DAYS('" + dateString + "')),\n";
            last = dateString;
        }
        System.out.println(str);
    }
}
